/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.worlds.ui.edge;

import java.awt.Color;
import java.awt.Font;
import java.awt.image.BufferedImage;
import java.io.IOException;
import net.worlds.AssetManager;
import net.worlds.WorldsException;
import net.worlds.lua.Event;
import net.worlds.object.GameObject;
import net.worlds.object.HiddenScript;
import net.worlds.object.Script;
import net.worlds.ui.UI;

/**
 *
 * @author Jacob
 */
public class EScriptingFrame extends EFrame{
    private final Color titleColor = new Color(197, 203, 217);
    private final Font titleFont = new Font("Arial", Font.BOLD, 16);
    private final Font boldFont = new Font("Arial", Font.BOLD, 12);
    private BufferedImage check;
    public EScriptingFrame(EComponent parent, int x, int y, int width, int height)
    {
        super(parent, x, y, width, height);
        try {
            check = AssetManager.loadImage("check.png", "check.png");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    @Override
    public void setVisible(boolean v)
    {
        super.setVisible(v);
        if(v)
        {
            if(UI.getNumberSelected() == 1)
            {
                ELabel eventsLabel = new ELabel("Events (click to edit)");
                eventsLabel.setX(5);
                eventsLabel.setY(5);
                eventsLabel.setColor(titleColor);
                eventsLabel.setFont(titleFont);
                addChild(1, eventsLabel);
                final GameObject obj = UI.getSelected().get(0);
                int y = 20;
                for(final Event e : obj.getEvents())
                {
                    ELabel eLabel = new ELabel(e.eventName);
                    eLabel.setHoverColor(new Color(177, 183, 207));
                    eLabel.setWidth(160);
                    eLabel.setHeight(16);
                    final String scriptName = e.eventName;
                    try {
                        if (obj.hasChild(scriptName) && obj.getChild(scriptName).getStringProp("Source").trim().split("\n").length > 0)
                        {
                            eLabel.setFont(boldFont);
                            EImage eCheck = new EImage(check);
                            eCheck.setX(10);
                            eCheck.setY(y+8);
                            addChild(1, eCheck);
                        }
                    }catch(WorldsException ex)
                    {
                        ex.printStackTrace();
                    }
                    eLabel.setX(20);
                    eLabel.setY(y);
                    eLabel.setActionListener(new EActionListener()
                    {
                        @Override
                        public void actionPerformed() {
                            try {
                                if (obj.hasChild(scriptName) && !(obj.getChild(scriptName) instanceof HiddenScript)) {
                                    obj.getChild(scriptName).remove();
                                }
                                String arguments = Event.getArguments(obj.getClass(), e.eventName);
                                if (!obj.hasChild(scriptName)) {
                                    HiddenScript script = (HiddenScript) obj.world.createObject("HiddenScript");
                                    script.setProp("Source", "", true);
                                    String[] parts = arguments.split(", ");
                                    StringBuilder builder = new StringBuilder();
                                    int i = 0;
                                    for(String arg : parts)
                                    {
                                        String[] subparts = arg.split(" ");
                                        builder.append(subparts[0]);
                                        if(i != parts.length - 1)
                                            builder.append(",");
                                        i++;
                                    }
                                    String argString = builder.toString();
                                    script.setProp("Name", scriptName, true);
                                    script.setProp("AutoPrepend", "link(script.Parent."+e.eventName+", function("+argString+") ", true);
                                    script.setProp("AutoAppend", "\nend)", true);
                                    //script.setProp("Source", "--Parameters you can use: "+arguments, true);
                                    script.setParent(obj);
                                }
                                UI.editScript((Script) obj.getChild(scriptName), "Parameters you can use: "+arguments);
                            } catch (WorldsException ex) {
                                ex.printStackTrace();
                            }
                        }                       
                    });
                    addChild(1, eLabel);
                    y += 16;
                }
            }
            else if(UI.getNumberSelected() > 1)
            {
                ELabel eventsLabel = new ELabel("Multiple objects selected");
                eventsLabel.setX(5);
                eventsLabel.setY(5);
                eventsLabel.setColor(titleColor);
                eventsLabel.setFont(titleFont);
                addChild(1, eventsLabel);
            }
        }
        else
        {
            for(EComponent c : getChildren(1))
                c.remove();
        }
    }
    public void reload()
    {
        if(visible)
        {
            setVisible(false);
            setVisible(true);
        }
    }
}
